package org.streets.eis.component.grid;


import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.model.IModel;

import com.inmethod.grid.column.AbstractColumn;
import org.streets.eis.component.ChangedNotifyType;
import org.streets.eis.component.foreigncombobox.ForeignComboBox;
import org.streets.eis.component.foreigncombobox.ForeignDefinition;
import org.streets.eis.component.model.ListItem;

public class ForeignComboBoxColumn extends EditableColumn {
	private static final long serialVersionUID = 1L;

	private ForeignDefinition definition ;
	
	private boolean canInput;

	private boolean onlySelectLeaf;
	
	private ChangedNotifyType changedNotifyType = ChangedNotifyType.NONE;

	@SuppressWarnings("unchecked")	
	public ForeignComboBoxColumn(String columnId, IModel headerModel, String propertyExpression, String sortProperty,ForeignDefinition definition) {
		this(columnId, headerModel, propertyExpression, sortProperty,definition,false);
	}

	@SuppressWarnings("unchecked")	
	public ForeignComboBoxColumn(String columnId, IModel headerModel, String propertyExpression, String sortProperty,ForeignDefinition definition,boolean canInput) {
		this(columnId, headerModel, propertyExpression, sortProperty,definition,canInput,false);
	}

	@SuppressWarnings("unchecked")	
	public ForeignComboBoxColumn(String columnId, IModel headerModel, String propertyExpression, String sortProperty,ForeignDefinition definition,boolean canInput,boolean onlySelectLeaf) {
		super(columnId, headerModel, propertyExpression, sortProperty);
		this.definition = definition;
		this.canInput = canInput;
		this.onlySelectLeaf = onlySelectLeaf;
	}

	@SuppressWarnings("unchecked")	
	public ForeignComboBoxColumn(String columnId, IModel headerModel, String propertyExpression,ForeignDefinition definition) {
		this(columnId, headerModel, propertyExpression,definition,false);
	}

	@SuppressWarnings("unchecked")	
	public ForeignComboBoxColumn(String columnId, IModel headerModel, String propertyExpression,ForeignDefinition definition,boolean canInput) {
		this(columnId, headerModel, propertyExpression,definition,canInput,false);
	}

	@SuppressWarnings("unchecked")	
	public ForeignComboBoxColumn(String columnId, IModel headerModel, String propertyExpression,ForeignDefinition definition,boolean canInput,boolean onlySelectLeaf) {
		super(columnId, headerModel, propertyExpression);
		this.definition = definition;
		this.canInput = canInput;
		this.onlySelectLeaf = onlySelectLeaf;
	}
	
	@SuppressWarnings("unchecked")	
	public ForeignComboBoxColumn(IModel headerModel, String propertyExpression, String sortProperty,ForeignDefinition definition) {
		this(headerModel, propertyExpression, sortProperty,definition,false);
	}

	@SuppressWarnings("unchecked")	
	public ForeignComboBoxColumn(IModel headerModel, String propertyExpression, String sortProperty,ForeignDefinition definition,boolean canInput) {
		this(headerModel, propertyExpression, sortProperty,definition,canInput,false);
	}

	@SuppressWarnings("unchecked")	
	public ForeignComboBoxColumn(IModel headerModel, String propertyExpression, String sortProperty,ForeignDefinition definition,boolean canInput,boolean onlySelectLeaf) {
		super(headerModel, propertyExpression, sortProperty);
		this.definition = definition;
		this.canInput = canInput;
		this.onlySelectLeaf = onlySelectLeaf;
	}
	
	@SuppressWarnings("unchecked")	
	public ForeignComboBoxColumn(IModel headerModel, String propertyExpression,ForeignDefinition definition) {
		this(headerModel, propertyExpression,definition,false);
	}

	@SuppressWarnings("unchecked")	
	public ForeignComboBoxColumn(IModel headerModel, String propertyExpression,ForeignDefinition definition,boolean canInput) {
		this(headerModel, propertyExpression,definition,canInput,false);
	}

	@SuppressWarnings("unchecked")	
	public ForeignComboBoxColumn(IModel headerModel, String propertyExpression,ForeignDefinition definition,boolean canInput,boolean onlySelectLeaf) {
		super(headerModel, propertyExpression);
		this.definition = definition;
		this.canInput = canInput;
		this.onlySelectLeaf = onlySelectLeaf;
	}

	
	@SuppressWarnings("unchecked")
	@Override
	protected FormComponent newEditableComponent(String id, final IModel model, IModel rowModel, AbstractColumn column){
		beforeNewComboBox(model,rowModel);
		return new ForeignComboBox(id, model,definition,canInput,onlySelectLeaf){
			private static final long serialVersionUID = 1L;
			@Override
			protected ChangedNotifyType getChangedNotifyType(){
				return ForeignComboBoxColumn.this.getChangedNotifyType();
			}
			@Override
			protected void onNoPopup(AjaxRequestTarget target){
				ForeignComboBoxColumn.this.onNoPopup(this,target);
			}			
			@Override
			protected void onItemChanged(ForeignComboBox comboBox,ListItem newValue){
				ForeignComboBoxColumn.this.onItemChanged(comboBox,newValue);
			}			
			@Override
			protected void onAjaxItemChanged(ForeignComboBox comboBox,ListItem newValue,AjaxRequestTarget target){
				ForeignComboBoxColumn.this.onAjaxItemChanged(comboBox,newValue,target);
			}
		};
	}
	
	public ChangedNotifyType getChangedNotifyType() {
		return changedNotifyType;
	}

	public void setChangedNotifyType(ChangedNotifyType changedNotifyType) {
		this.changedNotifyType = changedNotifyType;
	}	
	
	public boolean isCanInput() {
		return canInput;
	}

	public void setCanInput(boolean canInput) {
		this.canInput = canInput;
	}
	
	public ForeignDefinition getDefinition() {
		return definition;
	}

	public void setDefinition(ForeignDefinition definition) {
		this.definition = definition;
	}

	public boolean isOnlySelectLeaf() {
		return onlySelectLeaf;
	}

	public void setOnlySelectLeaf(boolean onlySelectLeaf) {
		this.onlySelectLeaf = onlySelectLeaf;
	}
	
	@SuppressWarnings("unchecked")
	protected void beforeNewComboBox(final IModel model, IModel rowModel){
	}
	
	protected void onNoPopup(FormComponent<?> component,AjaxRequestTarget target){
	}
	
	protected void onItemChanged(FormComponent<?> component,ListItem newValue){		
	}

	protected void onAjaxItemChanged(FormComponent<?> component,ListItem newValue,AjaxRequestTarget target){
	}
	
}
